import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:getwidget/getwidget.dart';

import '../controller.dart';
import 'dialogs/login_dialog.dart';

/// 主页的AppBar
class HomePageAppbar extends AppBar {
  @override
  _HomeAppbarState createState() => _HomeAppbarState();
}

class _HomeAppbarState extends State<HomePageAppbar> {
  final controller = Get.find<HomePageController>();

  Widget buildLeadWidget() {
    //判断登录状态

    return Obx(() {
      if (controller.state.token == "") {
        return IconButton(
          onPressed: () {
            showDialog(
              context: context,
              barrierDismissible: true,
              builder: (BuildContext context) => LoginDialog(),
            );
          },
          icon: GFAvatar(
            backgroundColor: Colors.white,
            child: Text(
              "登录",
              style: TextStyle(
                color: Colors.black,
              ),
            ),
          ),
        );
      }

      return IconButton(
        icon: Icon(Icons.account_circle_rounded),
        iconSize: 30,
        onPressed: () {
          showDialog(
            context: context,
            barrierDismissible: true,
            builder: (BuildContext context) => LoginDialog(),
          );
        },
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return GFAppBar(
      searchBar: true,
      leading: buildLeadWidget(),
      actions: [
        PopupMenuButton(
          itemBuilder: (context) => [
            PopupMenuItem(
              child: Text('设置'.tr),
              onTap: () {},
            ),
          ],
        ),
      ],
    );
  }
}
